package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.LoginUser;
import com.kpx.app.bean.User;
import com.kpx.app.cache.SQLiteCache;
import com.kpx.app.configs.BroadcastFilters;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MD5Util;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.datautils.FileUtils;
import com.kpx.app.utils.dbutils.SpUtil;
import com.kpx.app.utils.encrypt.Toolkit;
import com.kpx.app.utils.uiutils.IntentUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;
import com.kpx.app.wigdet.ClearEditText;
import com.tencent.bugly.crashreport.biz.UserInfoBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/12/19.
 * 登录
 */
public class ActivityLogin extends ActivityBase {

    @BindView(R.id.cet_count)
    ClearEditText cetCount;
    @BindView(R.id.cet_pwd)
    ClearEditText cetPwd;
    @BindView(R.id.txt_forget_pwd)
    TextView txtForgetPwd;
    @BindView(R.id.txt_register)
    TextView txtRegister;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.img_3)
    ImageView img_3;
    private boolean isShowpwd=false;
//
    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        UIUtils.setTitle(this, R.color.white, R.color.black_0a, "登录");
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        String name = SpUtil.getString(Constants.KEY_USER_PHONE);
        String pwd = SpUtil.getString(Constants.KEY_USER_PWD);
        if (!TextUtils.isEmpty(name)) {
            cetCount.setText(name);
        }
        if (!TextUtils.isEmpty(pwd)) {
            cetPwd.setText(pwd);
        }
    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    @OnClick({R.id.txt_forget_pwd, R.id.txt_register, R.id.btn_login, R.id.img_3})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.txt_forget_pwd://忘记密码
                toGetForgetPwd();
                break;
            case R.id.txt_register://立即注册
                toRegister();
                break;
            case R.id.btn_login://登录
                toLogin();
                break;
            case R.id.img_3:
                if(isShowpwd){
                    isShowpwd = false;
                    img_3.setBackgroundResource(R.drawable.login_right_see);
                    cetPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
//                    cetPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }else {
                    isShowpwd = true;
                    img_3.setBackgroundResource(R.drawable.see_pwd_icon);
                    cetPwd.setInputType(InputType.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_NORMAL);
//                    cetPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());

                }
                break;
        }
    }

    private void toLogin() {
        MyHashMap map = new MyHashMap();
        map.put("phone", cetCount.getText().toString().trim());
        map.put("pwd", MD5Util.encrypt(cetPwd.getText().toString().trim()));
        showLoadingDialog();
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    LoginUser user = (LoginUser) result.getData();
                    SQLiteCache.sharedInstance().put(user.getUSER().getDmId() + "", user);
                    SpUtil.putString(Constants.KEY_USER_PHONE, cetCount.getText().toString().trim());
                    SpUtil.putString(Constants.KEY_USER_TOKEN, user.getTOKEN());
                    SpUtil.putLong(Constants.KEY_USER_ID, user.getDmId());
                    SpUtil.putString(Constants.KEY_USER_DMID, user.getUSER().getDmId() + "");
                    SpUtil.putString(Constants.KEY_USER_PWD, cetPwd.getText().toString().trim());
                    ToastUtil.getInstance().showToast(ActivityLogin.this, "登陆成功");
                    sendBroadcast(new Intent(BroadcastFilters.BROADCAST_ACTION_LOGIN_SUCCESS_BACK));
                    finish();
                } else {
                    ToastUtil.getInstance().showToast(ActivityLogin.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(ActivityLogin.this, error);
            }
        }, LoginUser.class, HttpDataRequest.TYPE_OBJECT).POST(HttpDataReqUrl.URL_POST_LOGIN, true, map);
    }

    private void toRegister() {
        IntentUtil.gotoActivity(ActivityLogin.this, ActivityRegister.class);
    }

    private void toGetForgetPwd() {
        ActivityResetPwd.intype = 3;
        IntentUtil.gotoActivity(ActivityLogin.this, ActivityResetPwd.class);
    }

}
